package com.git.smp.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.git.smp.entity.ApplyServer;
import com.git.smp.entity.SmpServiceExtend;

public class ObjectComperUtil {
	
	/**
	 * 字段比较，返回不相同的字段
	 * @param obj1
	 * @param obj2
	 * @return
	 */
	public static List<String> objectComper(Object obj1,Object obj2){
		
		List<String> list = new ArrayList<>();
	     Class<?> obj1Class = obj1.getClass();
		
		Class<?> obj2Class = obj2.getClass();
		
		List<Field> AllFields  = new ArrayList<Field>();
		AllFields.addAll(Arrays.asList(obj1Class.getDeclaredFields()));
		AllFields.addAll(Arrays.asList(obj1Class.getSuperclass().getDeclaredFields()));
		try {
			AllFields.addAll(Arrays.asList(obj1Class.getSuperclass().getSuperclass().getDeclaredFields()));
		}catch (Exception e) {
			
		}

		for (Field tarfield : AllFields) {
			
			Object value =null;
			Object value2 =null;
			
			try {
				value = obj1Class.getMethod("get"+tarfield.getName().substring(0,1).toUpperCase()+tarfield.getName().substring(1)).invoke(obj1);
				value2 = obj2Class.getMethod("get"+tarfield.getName().substring(0,1).toUpperCase()+tarfield.getName().substring(1)).invoke(obj2);
			
				value = value==null?"":value;
				value2 = value==null?"":value2;
				
				if(!value.equals(value2)) {
					list.add(tarfield.getName());
				}
			} catch (Exception e) {
				
			} 
		}
		return list;
	}
	
	
	
public static void main(String[] args) {
	SmpServiceExtend smpService = new SmpServiceExtend();
	ApplyServer applyServer = new ApplyServer();
	smpService.setFunIntr("ddd");
	System.out.println(ObjectComperUtil.objectComper(smpService, applyServer));
	
	
	
}

}
